<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
import resize from './mixins/resize'
// import { number } from 'echarts'
export default {
  name: 'JspLineCharts',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    title: {
      type: String,
      default: '网站访问量'
    },
    xname: {
      type: String,
      default: '日期'
    },
    xdate: {
      type: Array,
      default() {
        return null
      }
    },
    xdata: {
      type: Array,
      default() {
        return null
      }
    },
    ymin: {
      type: Number,
      default: 0
    },
    ymax: {
      type: Number,
      default: 0
    },
    yname: {
      type: String,
      default: '访问量'
    },
    legendData: {
      type: Array,
      default() {
        return null
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    legendData() {
      this.initChart()
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestory() {
    if (!this.chart) return
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))
      const chartsOptions = {
        title: {
          // text: this.title,
          // subtext: '',
          x: 'center',
          textStyle: {
            fontStyle: 'normal',
            fontWeight: 'normal'
          }
        },
        // 图表距边框的距离,可选值：'百分比'¦ {number}（单位px）
        grid: {
          top: '20%',
          left: '3%',
          right: '3%',
          bottom: '3%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        xAxis: {
          name: this.xname,
          type: 'category',
          boundaryGap: true,
          data: this.xdate,
          axisLine: {
            lineStyle: {
              color: 'rgb(64, 158, 255)'
            }
          }
        },
        yAxis: {
          name: this.yname,
          type: 'value',
          min: this.ymin, // y轴刻度的最小值
          max: this.ymax, // y轴刻度的最大值
          splitNumber: 6 // y轴刻度间隔个数
        },
        legend: { data: this.legendData },
        series: this.xdata
      }
      this.chart.setOption(chartsOptions)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
